package ai.modules;

import ai.AIUtil;
import ai.engine.AICity;
import ai.engine.AIUnit;
import ai.engine.PossibleAssignment;
import ai.engine.Task;
import ai.engine.factories.TaskFactory;
import engine.GameEngine;
import util.Iterator;

public class ResourceAllocationModule {

	public final static void generateAssignments() {
		// listAsset contains a list of all assets (for instance ships)
		TaskFactory.listPossAssignment.clear();
		generateUnitAssignments();
		generateCityAssignments();
	}

	public final static void generateUnitAssignments() {
		Iterator it = GameEngine.currPlayer.units.iterator();
		AIUnit currentUnit;
		while (it.hasNext()) {
			currentUnit = (AIUnit) it.next();
			Iterator itTasks = AIUtil.getCurrentAIPlayer().tasks.iterator();
			Task currentTask;
			while (itTasks.hasNext()) {
				currentTask = (Task) itTasks.next();
				try {
					if (currentTask.taskDoerType.equals(AIUnit.class) && !currentUnit.played && currentTask.isTaskSuitable(currentUnit)) {
						TaskFactory.listPossAssignment.add(new PossibleAssignment(currentTask, currentUnit));
					}
				} catch (ClassCastException e) {
				}
				currentTask.assignedResources.clear();
			}
			currentUnit.resetTask();
		}
	}

	public final static void generateCityAssignments() {
		Iterator it = GameEngine.currPlayer.cities.iterator();
		AICity currentCity;
		while (it.hasNext()) {
			currentCity = (AICity) it.next();
			Iterator itTasks = AIUtil.getCurrentAIPlayer().tasks.iterator();
			Task currentTask;
			while (itTasks.hasNext()) {
				currentTask = (Task) itTasks.next();
				try {
					if (currentTask.taskDoerType.equals(AICity.class) && currentTask.isTaskSuitable(currentCity)) {
						TaskFactory.listPossAssignment.add(new PossibleAssignment(currentTask, currentCity));
					}
				} catch (ClassCastException e) {
				}
				currentTask.assignedResources.clear();
			}
			currentCity.resetTask();
		}
	}

	public final static void sortAssignments() {
		ai.Sort.quickSort(TaskFactory.listPossAssignment, 0, TaskFactory.listPossAssignment.size() - 1);
	}

	public final static void assignTasks() {
		Iterator it = TaskFactory.listPossAssignment.iterator();
		PossibleAssignment currentPA;
		while (it.hasNext()) {
			currentPA = (PossibleAssignment) it.next();
			if (currentPA.possibleTaskDoer.getTask() == null && currentPA.task.assignedResources.size() < currentPA.task.resourcesMax) {
				// System.out.println(assignment.possibleTaskDoer + " is
				// assigned to " + assignment.task);
				currentPA.task.assign(currentPA.possibleTaskDoer);
				currentPA.possibleTaskDoer.setTask(currentPA.task);
				if (currentPA.task.assignedResources.size() >= currentPA.task.resourcesMin)
					currentPA.task.status = ai.AIConstant.STATUS_IN_PROGRESS;
			}
		}
	}

	public static final String getString() {
		StringBuffer res = new StringBuffer("AI Resouce Allocation Module - turn ").append(GameEngine.turn).append(" - civ ").append(GameEngine.currPlayer.civilization).append(" : \n");
		Iterator it = TaskFactory.listPossAssignment.iterator();
		PossibleAssignment currentPA;
		while (it.hasNext()) {
			currentPA = (PossibleAssignment) it.next();
			res.append(currentPA.toString());
		}
		return res.toString();
	}

}
